package com.tianqu.tms.www.finance.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.IntegralRegister;
import com.tianqu.tms.entity.TransferFinanceInfo;
import com.tianqu.tms.www.finance.actionform.PrepaymentSummaryForm;
import com.tianqu.tms.www.finance.viewhelper.FinanceSettlementSummaryViewHelper;
import com.tianqu.tms.www.finance.viewhelper.PrepaymentSummaryViewHelper;

public class PrepaymentSummaryExportExcelAction extends TmsAction {
	private Logger logger = Logger.getLogger(PrepaymentSummaryExportExcelAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws Exception {
        logger.debug(this.getClass().getName() + "#execute was called.");
		
		String startDate = uc.getParameter("startDate");
		String endDate = uc.getParameter("endDate");
		String type = uc.getParameter("type");
		String pType = uc.getParameter("pType");
		
		PrepaymentSummaryForm inputForm = (PrepaymentSummaryForm)uc.getInputForm();
		//
		IntegralRegister integralRegister = IntegralRegister.getInstance().findIntegralRegisterInfo(inputForm.getMemberId(), inputForm.getDateFrom().replace("-", ""), inputForm.getDateTo().replace("-", ""));
		//
		PrepaymentSummaryViewHelper view = new PrepaymentSummaryViewHelper();
		view.setAction(inputForm.getAction());
		view.setDateFrom(inputForm.getDateFrom());
		view.setDateTo(inputForm.getDateTo());
		view.setMemberId(inputForm.getMemberId());
		view.setCount(integralRegister.getCount());
		view.setAttnName(integralRegister.getAttnName());
		view.setShortName(integralRegister.getShortName());
		view.setTotalTransferPrice(integralRegister.getTotalTransferPrice());
		view.setSumTransferPrice(integralRegister.getSumTransferPrice());
		view.setInfo(integralRegister.getInfo());
		
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("预收款汇总表");
		HSSFRow row = sheet.createRow((int) 0);
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
		style.setBorderBottom((short) 1);
		style.setBorderTop((short) 1);
		style.setBorderLeft((short) 1);
		style.setBorderRight((short) 1);

		sheet.setDefaultRowHeightInPoints(10);
		sheet.setDefaultColumnWidth(17);

		HSSFFont font = wb.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		font.setFontHeightInPoints((short) 8);
		style.setFont(font);

		HSSFCellStyle cellStyle = wb.createCellStyle();
		HSSFFont titleFont = wb.createFont();
		titleFont.setFontName("宋体");
		titleFont.setFontHeight((short) 200);
		titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

		cellStyle.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
		cellStyle.setBorderBottom((short) 1);
		cellStyle.setBorderTop((short) 1);
		cellStyle.setBorderLeft((short) 1);
		cellStyle.setBorderRight((short) 1);
		cellStyle.setFont(titleFont);
		HSSFCell cell = row.createCell((short) 0);
		cell.setCellValue("序号");
		cell.setCellStyle(cellStyle);

		HSSFCell cell1 = row.createCell((short) 1);
		cell1.setCellValue("会员编号");
		cell1.setCellStyle(cellStyle);

		HSSFCell cell2 = row.createCell((short) 2);
		cell2.setCellValue("交费人");
		cell2.setCellStyle(cellStyle);

		HSSFCell cell3 = row.createCell((short) 3);
		cell3.setCellValue("所属商户");
		cell3.setCellStyle(cellStyle);
 
		HSSFCell cell4 = row.createCell((short) 4);
		cell4.setCellValue("充值类型");
		cell4.setCellStyle(cellStyle);
		
		HSSFCell cell5 = row.createCell((short) 5);
		cell5.setCellValue("充值金额");
		cell5.setCellStyle(cellStyle);
		
		HSSFCell cell6 = row.createCell((short) 6);
		cell6.setCellValue("充值时间");
		cell6.setCellStyle(cellStyle);
		 
		List<IntegralRegister> list = integralRegister.getInfo();
		for (int i=1;i<list.size()+1;i++) {
			IntegralRegister dto = (IntegralRegister) list.get(i-1);
			row = sheet.createRow(i);
			HSSFCell c0 = row.createCell((short) 0);
			c0.setCellStyle(style);
			c0.setCellValue(dto.getNo());
			
			HSSFCell c1 = row.createCell((short) 1);
			c1.setCellStyle(style);
			c1.setCellValue(dto.getMemberId());
			
			HSSFCell c2 = row.createCell((short) 2);
			c2.setCellStyle(style);
			c2.setCellValue(dto.getAttnName());
			
			HSSFCell c3 = row.createCell((short) 3);
			c3.setCellStyle(style);
			c3.setCellValue(dto.getShortName()); 
			
			HSSFCell c4 = row.createCell((short) 4);
			c4.setCellStyle(style);
			c4.setCellValue(dto.getChargeTypeStr()); 
			
			HSSFCell c5 = row.createCell((short) 5);
			c5.setCellStyle(style);
			c5.setCellValue(dto.getTransferPrice()); 
			
			HSSFCell c6 = row.createCell((short) 6);
			c6.setCellStyle(style);
			c6.setCellValue(dto.getInsertTimeStr()); 
		}
		try {
			File file = new File(toUtf8String("预收款汇总表"+DateUtil.getCurrentTimeStr()+".xls"));
			OutputStream fout = new FileOutputStream(file);
			wb.write(fout);
			getResponse(file, response);
		} catch (Exception e) {
			logger.error("PrepaymentSummaryExportExcelAction",e);
		}
		request.setAttribute(TmsConstants.VIEWHELP, view);
		
		return null;
	}
}
